﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using CrystalDecisions.Web;
using CrystalDecisions.CrystalReports.Engine;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;
namespace Recon.Portal.WebUI.Modules
{
    public partial class ExcelReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //fetch data based on report search criteria
                ReportSearchCriteria criteria = (ReportSearchCriteria)Session["ReportSearchCriteria"];
                Lists lstReportData = new Lists();
                DataTable reportData = lstReportData.GetReportData(criteria);

                if (criteria.ReportType.ToString() == ReportSearchCriteria.Type.Invoice.ToString())
                {
                    lblHeader.Text = "Invoice ";
                }
                else if (criteria.ReportType.ToString() == ReportSearchCriteria.Type.Statement.ToString())
                {
                    lblHeader.Text = "Statement ";
                }
                if (reportData != null)
                {
                    if (reportData.Rows.Count > 0)
                    {
                        dgReport.DataSource = reportData;
                        dgReport.Visible = true;
                        dgReport.DataBind();

                        btnCancel.Visible = true;
                        btnDwnExcelFormat.Visible = true;
                        lblMessage.Text = "";
                        lblMessage.Visible = false;
                    }
                    else
                    {
                        lblMessage.Text = "No Record Found!";
                        dgReport.Visible = false;
                        btnCancel.Visible = false;
                        btnDwnExcelFormat.Visible = false;
                    }
                }
                else
                {
                    lblMessage.Text = "No Record Found!";
                    dgReport.Visible = false;
                    btnCancel.Visible = false;
                    btnDwnExcelFormat.Visible = false;
                }
                btnCancel.Visible = true;
            }   
        }

        protected void btnDwnExcelFormat_Click(object sender, EventArgs e)
        {
            //fetch data based on report search criteria
            ReportSearchCriteria criteria = (ReportSearchCriteria)Session["ReportSearchCriteria"];
            Lists lstReportData = new Lists();
            DataTable reportData = lstReportData.GetReportData(criteria);
           
            dgReport1.DataSource = reportData;
            dgReport1.DataBind();
            

            Response.Clear();
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";

            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);

            DataGrid dg = new DataGrid();

            dg = (DataGrid)Page.FindControl("dgReport1");
            dg.GridLines = GridLines.None;
            dg.HeaderStyle.Font.Bold = true;
            dg.DataBind();
            dg.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
        }
    }
}